﻿#pragma once
#include <iostream>
#include <mutex>
#include <vector>
#include <QString>

struct AVCodecContext;
struct AVPacket;
struct AVFrame;

/// <summary>
/// 编解码器基类
/// </summary>
class YCodec
{

public:
	/// <summary>
	/// 创建编解码器上下文
	/// </summary>
	/// <param name="codec_id">编解码器的id</param>
	/// <param name="is_encode">是否是编码器</param>
	/// <returns></returns>
	static AVCodecContext* Create(int codec_id,bool is_encode);
	/// <summary>
	/// 设置编解码器上下文
	/// </summary>
	/// <param name="context">编解码器上下文</param>
	void setContext(AVCodecContext* context);
	//设置线程安全的编码参数
	bool SetOpt(const char* key, const char* val);
	bool SetOpt(const char* key, int val);

	/// <summary>
	/// 打开编解码器
	/// </summary>
	/// <returns></returns>
	bool Open();

	/// <summary>
	/// 根据AVCodecContext创建一个默认的AVFrame
	/// </summary>
	AVFrame* CreateFrame();

	void PrintError(int err, QString errMsgHead);

	//清理资源
	virtual void Clear();

	YCodec();
	~YCodec();
protected:
	AVCodecContext *m_context = nullptr;//编解码器上下文
	std::mutex m_mutex;//互斥锁
};
